<html>
<head><title>Lab 1a</title></head>

<body>
<h1>Lab 1A Calculator</h1>
<br>
<p>
<form method="GET">
<input type="text" name="expr"><br>
<input type="submit" value="Calculate">
</form>
</p>


<?php
if($_GET["expr"])
    $test=$_GET["expr"];
else
	exit;
// Remove whitespaces
$test = preg_replace('/\s+/', '', $test);


$number = '\d+([.]\d+)?'; 
$operators = '([+\/*-]|[*\/]-)'; 
$regexp = '/^-?'.$number.'('.$operators.$number.')*$/';


if (preg_match($regexp, $test))
{
	if(preg_match('/'.$number.'\/0([^.])?/', $test))
	{
		$result = false;
		echo "Error: Dividing by ZERO <br>";
    }
	else
	{
		@eval('$result = '.$test.';');
		echo "Result: " .$result;
	}
}
else
{
    $result = false;
	echo "Error: Invalid input";
}
?>
